package org.example.im.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.im.bean.protocol.response.LoginResponsePacket;
import org.example.im.bean.session.Session;
import org.example.im.common.util.SessionUtil;
import org.springframework.stereotype.Component;

/**
 * @author dingrui
 * @since 2022/2/10
 * @description 登录响应
 */
@Slf4j
@Component
public class LoginHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket response) throws Exception {
        String userId = response.getUserId();
        String userName = response.getUserName();
        if (response.isSuccess()) {
            SessionUtil.bindSession(ctx.channel(), Session.builder().userId(userId).userName(userName).build());
            System.out.println("[C-Login] 登录成功 userId=[" + userId + "] userName=[" + userName + "]");
        } else
            System.out.println("[C-Login] 登录失败 userName={" + userName + "} 详情:{" + response.getMsg() + "}");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("[C-Login] 客户端连接被关闭");
    }
}
